<template>
	<view class="grace-add-list">
		<view class="items" v-for="(item, index) in imgLists" :key="index">
			<image :src="item.url" :data-imgurl="item.url" @tap="showImgs"></image>
			<!-- <view v-if="isshow" class="remove" @tap="removeImg(item)" :id="'grace-items-img-' + index"></view> -->
			<image v-if="isshow" src="../../static/assets/simg/zg_slices/icon-wrong.png" class="remove" @tap="removeImg(item)"></image>
			
			<view class="show-progress"></view>
		</view>
		<view class="items add-btn" @tap="addImg" v-if="imgLists.length < maxFileNumber && canadd">
			<view class="add-btn-add">+</view>
			<view class="add-btn-text">{{ btnName }}</view>
		</view>
	</view>
</template>
<script>
import {tempScanCodeShortData} from 'Command/tempScanCodeShortData.js';
import {tempTable} from 'Command/tempTable.js';
import {Commnd} from 'Command/Commnd.js';
export default {
	props: {
		maxFileNumber: {
			type: Number,
			default: 9
		},
		btnName: {
			type: String,
			default: '添加照片'
		},
		ProcessID: {
			type: String,
			default: ''
		},
		items: {
			type: Array,
			default: function() {
				return [];
			}
		},
		isshow: {
			type: Boolean,
			default: true
		},
		uploading: {
			type: Boolean,
			default: false
		},
		uploadServerUrl: {
			type: String,
			default: ''
		},
		canadd: {
			type: Boolean,
			default: true
		}
	},
	data() {
		return {
			imgLists: [],
			uploadindex: 0
		};
	},
	created: function() {
		this.imgLists = this.items;
		this.getNetwork();
	},
	watch: {
		imgLists: function(newVal, oldVal) {
			this.$emit('change', newVal);
		},
		uploading: function(newVal, oldVal) {
			if (newVal && !oldVal) {
				this.upload(this, 0);
			}
		}
	},
	methods: {
		getNetwork(){
			let _this = this
			uni.getNetworkType({
				success(e) {
				if (e.networkType == 'none') {
					// console.log('_this.ProcessID',_this.ProcessID);
					_this.networkcall();
				}
			}
			});
		},
		networkcall(){
			tempScanCodeShortData.GetScanCodeShortDataBy_ForeignKey(this.ProcessID).then(e=>{
				e.data.forEach(element=>{
					element.url = element.path
				})
				this.imgLists = e.data;
			})
		},
		upload: function(_self, index) {},
		addImg: function() {
			var _self = this;
			var num = this.maxFileNumber - _self.imgLists.length;
			uni.chooseImage({
				count: 1,
				sizeType: ['compressed'],
				success: function(res) {
					uni.getNetworkType({
						success(e) {
							if (e.networkType == 'none') {
								let arrays = [];
								res.tempFiles.forEach(element => {
									// let tempArry = Commnd.convertClass(tempTable.ScanCodeShortDetails);
									let tempArry = tempTable.ScanCodeShortDetails;
									tempArry = element;
									tempArry[tempTable.CommanColumn + tempTable.CommanColumn_ForeignKey] = _self.ProcessID;
									tempArry[tempTable.CommanColumn] = Commnd.randomString();
									arrays.push(tempArry)
								});
								console.log(arrays);
								tempScanCodeShortData.AddScanCodeShortDataDetails(arrays);
								_self.getNetwork();
							}
						}
					});
					uni.uploadFile({
						url: 'http://api.dev.lv-yan.com/api/core/UploadFile',
						filePath: res.tempFiles[0].path,
						name: 'tempFiles',
						// files: res.tempFiles,
						file: res.tempFiles[0],
						formData: {
							ProcessID: _self.ProcessID,
							FileName: 'tempFiles',
							DataType: 'mobile',
							TenantID: _self.changeDataparse().TenantID
						},
						success: function(uploadRes) {
							_self.GetFileList();
						}
					});
					uni.hideLoading();
				},
				fail: function() {
					uni.hideLoading();
				}
			});
		},
		removeImg: function(item) {
			let _this = this
			uni.getNetworkType({
				success(e) {
					if (e.networkType == 'none') {
						tempScanCodeShortData.DeleteScanCodeShortDataDetailsOne(item[tempTable.CommanColumn]).then(res=>{
							_this.networkcall();
						}) 
					}else{
						let Id = item.url.split('=')[1];
						_this.GET({ ID: Id }, 'core/DeleteFile', res => {
							_this.GetFileList();
						});
					}
				}
			});
			
		},
		GetFileList() {
			this.imgLists = [];
			this.GET({ ProcessID: this.ProcessID, TenantID: this.changeDataparse().TenantID }, 'core/GetFileList', res => {
				for (var i = 0; i < res.data.length; i++) {
					this.imgLists.push({ url: this.image + res.data[i].ID });
				}
			});
		},
		showImgs: function(e) {
			var prvImgs = [];
			this.imgLists.forEach(function(item, key) {
				prvImgs.push(item.url);
			});
			var currentImg = e.currentTarget.dataset.imgurl;
			uni.previewImage({
				urls: prvImgs,
				current: currentImg
			});
		},
		uploadFail: function() {
			this.$emit('uploaderror');
		},
		//重置进度
		resetprogress: function(index) {
			setTimeout(() => {
				this.imgLists[index].progress = 0;
			}, 1000);
		}
	}
};
</script>
<style>
@font-face {
	font-family: 'gFont';
	src: url('https://at.alicdn.com/t/font_1350431_73a79xoewq7.ttf') format('truetype');
}
.grace-add-list {
	display: flex;
	flex-wrap: wrap;
}
.grace-add-list .add-btn {
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
}
.grace-add-list .add-btn-text {
	font-size: 26rpx;
	line-height: 36rpx;
	text-align: center;
	color: #999999;
	width: 100%;
}
.grace-add-list .add-btn-add {
	font-size: 100rpx !important;
	height: 80rpx;
	line-height: 80rpx;
	margin-bottom: 20rpx;
	color: #999999;
}
.grace-add-list > .items {
	width: 220rpx;
	height: 220rpx;
	overflow: hidden;
	margin: 5px 0;
	background: #f3f4f5;
	font-size: 0;
	position: relative;
	border-radius: 10rpx;
	margin-right: 13rpx;
}
.grace-add-list > .items image {
	width: 220rpx;
	height: 220upx;
}
.grace-add-list > .items .remove {
	font-family: 'gFont';
	width: 50rpx;
	height: 50rpx;
	line-height: 50rpx;
	text-align: center;
	font-size: 22rpx;
	border-radius: 50rpx;
	position: absolute;
	z-index: 0;
	right: 5rpx;
	top: 5rpx;
}
.grace-add-list > .items .remove:after {
	content: '\e6dd';
}
</style>
